﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Rtz2Xlsx
{
    public partial class FrmByteSplitHelper : Form
    {
        public FrmByteSplitHelper()
        {
            InitializeComponent();
            comboBoxEncoding.Text = "Unicode";
        }

        private void buttonString2Ascii_Click(object sender, EventArgs e)
        {
            string str = "";
            string str0 = "";
            switch (comboBoxEncoding.Text)
            {
                case "Unicode":
                    for (int i = 0; i < richTextBoxString.Text.Length; i++)
                    {
                        str = str + (int)richTextBoxString.Text[i] + " ";
                    }
                    str = str.Trim();
                    richTextBoxAscii.Text = str;
                    break;
                case "byte":
                    str = "";
                    str0 = richTextBoxString.Text;
                    for (int i = 0; i < str0.Length; i++)
                    {
                        char[] charArr = new char[1];
                        charArr[0] = str0[i];
                        byte[] btArr = Encoding.GetEncoding("GBK").GetBytes(charArr);
                        foreach (byte item in btArr)
                        {
                            str = str + item.ToString() + "|";
                        }
                        str = str.Remove(str.Length - 1);
                        str += " ";
                    }
                    str = str.Trim();
                    richTextBoxAscii.Text = str;
                    break;
            }
        }

        private void buttonAscii2String_Click(object sender, EventArgs e)
        {
            string[] strArr = richTextBoxAscii.Text.Split(' ');
            string str = "";
            switch (comboBoxEncoding.Text)
            {
                case "Unicode":
                    foreach (string item in strArr)
                    {
                        str += (char)(Int32.Parse(item));
                    }
                    richTextBoxString.Text = str;
                    break;
                case "byte":
                    foreach (string item in strArr)
                    {
                        string[] strArr1 = item.Split('|');
                        byte[] byteArr = new byte[strArr1.Length];
                        for (int i = 0; i < strArr1.Length; i++)
                        {
                            byteArr[i] = Byte.Parse(strArr1[i]);
                        }
                        str += Encoding.GetEncoding("GBK").GetString(byteArr);
                    }
                    richTextBoxString.Text = str;
                    break;
            }
        }

        private void richTextBoxString_TextChanged(object sender, EventArgs e)
        {
            labelCharNum.Text = "字符个数：" + Convert.ToString(richTextBoxString.Text.Length) + "个";

            byte[] bytes = Encoding.GetEncoding("GBK").GetBytes(richTextBoxString.Text);
            labelCharLength.Text = "byte数：" + bytes.Length;
        }

        private void comboBoxEncoding_SelectionChangeCommitted(object sender, EventArgs e)
        {
            labelAscii.Text = comboBoxEncoding.Text + ":";
        }
    }
}
